/*
  Created on Jul 6, 2005
  (C) 2005 
  @author Michael Long, Adam D. Bradley

  This program is free software; you can redistribute it and/or
  modify it under the terms of Version 2 of the GNU General Public 
  License.
  
  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.
  
  You should have received a copy of the GNU Library General Public
  License along with this library; if not, write to the Free
  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

package snobject;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.Raster;
import snbenchlib.Base64;
import snbenchlib.Util;

import org.w3c.dom.*;
import snobject.snObject;

public class snImage extends snObject {

	/**
	 * instantiate a new snImage with given content
	 * @param _content
	 */
	public snImage(BufferedImage _content) {
		super();
		
		content = _content;
	}

	public String getXmlMimeType() { return MimeTypes.IMAGE; }
	public String getWebMimeType() { return "image/png"; }
	public String getBinaryMimeType() { return "image/png"; }
	
	public BufferedImage getImage(){
		return (BufferedImage)content;
	}

	public void setImage(BufferedImage _content){
		content  = _content;
	}
	
	public BufferedImage getImageCopy(){
	   BufferedImage buffImgNew = new BufferedImage(((BufferedImage)content).getWidth(),((BufferedImage)content).getHeight(),((BufferedImage)content).getType());
	   Graphics2D g = buffImgNew.createGraphics();
	   g.drawImage(((BufferedImage)content),null,null);

	   return buffImgNew;		
	}
	
	public void xmlWrite(java.io.OutputStream out,int depth) throws java.io.IOException {
		xmlWritePre(out,depth);
		java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
		binaryWrite(baos);
		out.write((Base64.encodeBytes(baos.toByteArray()).getBytes()));
		xmlWritePost(out,0);
	}
	
	public void webWrite(java.io.OutputStream out,int depth) throws java.io.IOException {
		javax.imageio.ImageIO.write((BufferedImage) content, "png", out);
	}
	public void binaryWrite(java.io.OutputStream out) throws java.io.IOException {
		webWrite(out,0);
	}

	public static snImage xmlParse(Element e) 
	throws step.parse.SyntaxError {
		if (!e.getTagName().equals("snobject"))
			throw new step.parse.SyntaxError(
				"Expected <snobject>, found <" +
				e.getTagName() + "> instead");
		Node n = e.getFirstChild();
		if (n.getNodeType() != Node.TEXT_NODE)
			throw new step.parse.SyntaxError(
				"<snobject> for snbench/image " + 
				"must contain ONLY base64-encoded " + "text!");
		java.io.ByteArrayInputStream bais = 
			new java.io.ByteArrayInputStream(
				Base64.decode(((Text) n).getData())
				);
		try {
			BufferedImage img = javax.imageio.ImageIO.read(bais);
			Raster raster = img.getRaster();
			BufferedImage newimg = new BufferedImage(raster.getWidth(), raster.getHeight(), 5);
			newimg.setData(raster);
			return new snImage(newimg);
		} catch (java.io.IOException ex) {
			throw new step.parse.SyntaxError(
				"Unknown failure interpreting image: " + 
				ex.toString());
		}
	}
}
